package com.service.school.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.base.BaseDaoI;
import com.model.TCollege;
import com.pageModel.College;
import com.pageModel.DataGrid;
import com.service.school.CollegeServiceI;

@Service(value = "collegeService")
public class CollegeServiceImpl implements CollegeServiceI {

	private BaseDaoI<TCollege> collegeDao;
	
	public BaseDaoI<TCollege> getCollegeDao() {
		return collegeDao;
	}
	@Autowired
	public void setCollegeDao(BaseDaoI<TCollege> collegeDao) {
		this.collegeDao = collegeDao;
	}

	@Override
	public List<TCollege> getCollegeCmb(College college) {
		String hql="from TCollege t where t.isEnble ='是'";
		List<TCollege> l = collegeDao.find(hql);
		return l;
	}
	@Override
	public DataGrid datagrid(College college) {
		DataGrid dg = new DataGrid();
		String hql="from TCollege t where 1=1 ";
		if(college.getCollegeId()!=null&&!college.getCollegeId().trim().equals("")){
			hql+="and t.collegeId='"+college.getCollegeId()+"'";
		}
		if(college.getIsEnble()!=null&&!college.getIsEnble().trim().equals("")){
			hql+=" and t.isEnble='"+college.getIsEnble()+"'";
		}
		if(college.getCollegeName()!=null&&!college.getCollegeName().trim().equals("")){
			hql+=" and t.collegeName='"+college.getCollegeName()+"'";
		}
		String totalHql = "select count(*) " + hql;
		List<TCollege> l = collegeDao.find(hql, college.getPage(), college.getRows());
		List<College> nl = new ArrayList<College>();
		if (l != null && l.size() > 0) {
			for (TCollege t : l) {
				College u = new College();
				BeanUtils.copyProperties(t, u);
				nl.add(u);
			}
		}
		dg.setTotal(collegeDao.count(totalHql));
		dg.setRows(nl);
		return dg;
	}
	@Override
	public boolean add(String collegeId, String collegeName, String isEnble) {
		Long count=collegeDao.count("select count(*) from TCollege obj where obj.collegeId=?", new Object[]{collegeId});
		if (count != null && count > 0) {
			 return  false;
		}
		TCollege t=new TCollege();
		t.setId(UUID.randomUUID().toString());
		t.setCollegeId(collegeId);
		t.setCollegeName(collegeName);
		t.setSchoolName("科技学院");
		t.setIsEnble(isEnble);
		collegeDao.save(t);
		return true;
	}
	@Override
	public void remove(String collegeIds) {
		String hql="delete TCollege t where t.collegeId in(";
		hql+="'"+collegeIds.trim()+"')";
		collegeDao.executeHql(hql);
		
	}
	@Override
	public void edit(College college) {
		TCollege t=collegeDao.get(TCollege.class,college.getId());
		BeanUtils.copyProperties(college, t,new String[]{"schoolName"});
	}

}
